package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.QlNewBookAdapter;
import com.shuorikeji.huohuo.adapter.YouLikeAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.AllBookMainBean;
import com.shuorikeji.huohuo.bean.BookDetailInfoBean;
import com.shuorikeji.huohuo.bean.BookDetailInfoTempBean;
import com.shuorikeji.huohuo.bean.TjBookMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.shuorikeji.huohuo.view.GridDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class WjzxActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ll_h)
    LinearLayout llH;
    @BindView(R.id.rv_book)
    RecyclerView rvBook;
    @BindView(R.id.rv_c_you_like_book)
    RecyclerView rvCyouLike;

    private int curPage = 1;
    private int pageSize = 8;
    private List<BookDetailInfoTempBean> wjzxBookList = new ArrayList<>();//完结甄选书籍
    private QlNewBookAdapter adapter;
    private List<BookDetailInfoBean> yLikeBookList = new ArrayList<>();
    private YouLikeAdapter youLikeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_wjzx);
    }

    @Override
    protected void initData() {
        initAdapter();
        getAllBook(false, 6, -1, -1, 1, -1, 2, -1, 0, -1, -1, -1);
        getCYouLike();
        initClick();
    }

    private void initAdapter() {
        rvBook.setLayoutManager(new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false));
        rvBook.addItemDecoration(new GridDividerItemDecoration(60));
        adapter = new QlNewBookAdapter(WjzxActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookDetailInfoTempBean data = (BookDetailInfoTempBean) object;
                        Intent intent = new Intent(WjzxActivity.this, BookInfoActivity.class);
                        intent.putExtra("BOOK_ID", data.getId());
                        startActivity(intent);
                        break;
                }
            }
        });
        rvBook.setAdapter(adapter);

        rvCyouLike.setLayoutManager(new LinearLayoutManager(WjzxActivity.this, LinearLayoutManager.VERTICAL, false));
        rvCyouLike.addItemDecoration(new SpacesItemDecoration(0, 0, 10, 0));
        youLikeAdapter = new YouLikeAdapter(WjzxActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookDetailInfoBean data = (BookDetailInfoBean) object;
//                        Intent intent = new Intent(HomeActivity.this, BookInfoActivity.class);
//                        intent.putExtra("BOOK_ID", data.getId());
//                        startActivity(intent);
                        break;
                }
            }
        });
        rvCyouLike.setAdapter(youLikeAdapter);
    }

    /**
     * 获取所有书籍
     *
     * @param flagTemp 1新书严选
     *                 2潜力新书
     *                 3新书首发
     *                 4精选
     *                 5banner
     *                 6完结甄选
     *                 7热门标签书籍
     *                 8会员书库
     *                 9大家都在看
     */
    private void getAllBook(boolean show, int flagTemp, int sort, int score, int winnow, int banner, int completed, int tagId,
                            int is_vip, int yjTypeId, int ejTypeId, int sjTypeId) {
        mPresenter.getAllBook(WjzxActivity.this, flagTemp, sort, pageSize, curPage, "", yjTypeId,
                ejTypeId, -1, tagId, completed, is_vip, -1, score,
                winnow, banner, "", sjTypeId, show, new ClickCallBack() {
                    @Override
                    public void onClick(int flag, Object object) {
                        switch (flag) {
                            case 1:
                                AllBookMainBean data = (AllBookMainBean) object;
                                if (flagTemp == 6) {
                                    if (curPage == 1) {
                                        wjzxBookList.clear();
                                    }
                                    wjzxBookList.addAll(data.getData().getData());
                                    adapter.setData(wjzxBookList);
                                }
                                break;
                            case 2:
                                String msg = (String) object;
                                ToastShowUtil.showToastCenter(WjzxActivity.this, msg);
                        }
                    }
                });
    }

    /**
     * 猜你喜欢
     */
    private void getCYouLike() {
        mPresenter.cYouLike(WjzxActivity.this, 1, 50, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        TjBookMainBean data = (TjBookMainBean) object;
                        yLikeBookList.clear();
                        yLikeBookList.addAll(data.getData().getData());
                        youLikeAdapter.setData(yLikeBookList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(WjzxActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        llH.setOnClickListener(view -> {
            //换一换
        });
    }
}